from templates.config import db
import jieba
from model.video import get_video


def couWord(input_txt_list):
    my_dict = {}  # 空字典 来保存单词出现的次数
    for txt in input_txt_list:
        print(txt)
        for v in jieba.lcut(txt['input_txt'], cut_all=True):
            if my_dict.get(v):
                my_dict[v] += 1
            else:
                my_dict[v] = 1
    return my_dict


def grade(dict, label):
    score = 0
    lt = label[1:-1].split(',')
    print(lt)
    for word in lt:
        if dict.get(word):
            score += dict[word]
    return score


def recommend(username, chat_id):
    # 获取集合对象
    collection = db[username]
    # 检索所有文档
    cursor = collection.find({'chat_id': chat_id}, {'input_txt': 1, '_id': 0})
    my_dict = couWord(cursor)
    data = get_video()
    d = data[0]
    score = 0
    for i in range(len(data)):
        print(data[i][7])
        g = grade(my_dict, data[i][7])
        if g > score:
            score = g
            d = data[i]
        print('video{}:{}'.format(i+1, g))
    print('recommend:{}'.format(d))
    return d
